package com.qq.BFMRSE.component;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

import com.qq.BFMRSE.encrypt.encryotHelper;
import com.qq.BFMRSE.extractWord.SelectWord;
import com.qq.BFMRSE.extractWord.luceneOperation;
import com.qq.BFMRSE.index.buildIndex;

import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class fileUpDeaProgress extends JFrame implements ActionListener, ChangeListener{
	private JLabel luIndexJLabel;
	private JProgressBar luIndexProgressBa;
	private JLabel extKWJlabel;
	JProgressBar extKWProgressBar;
	private JLabel indexJLabel;
	private JProgressBar indexProgressBar;
	private Timer luIndextimer;
	private Timer extTimer;
	private Timer indexTimer;
	private luceneOperation lOperation;
	private SelectWord selectWord;
	private buildIndex buIndex;
	private boolean luFinish;
	private boolean extFinish;
	private boolean indexFinish;
	
	/**
	 * Create the frame.
	 */
	public fileUpDeaProgress(luceneOperation lOperation,SelectWord selectWord,buildIndex buIndex) {
		init();
		this.lOperation=lOperation;
		this.selectWord=selectWord;
		this.buIndex=buIndex;
		luFinish=false;
		extFinish=false;
		indexFinish=false;
		this.setTitle("处理进度");
		getContentPane().setLayout(null);
		setSize(500, 200);
		setVisible(true);
		setResizable(true);// 禁止拉边框拉长拉断
		setLocationRelativeTo(null);
		
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void init(){
		luIndexJLabel = new JLabel("文本集分析：");
		luIndexJLabel.setBounds(32, 26, 80, 15);
		getContentPane().add(luIndexJLabel);
		luIndexProgressBa = new JProgressBar();
		luIndexProgressBa.setBounds(137, 26, 311, 22);
		luIndexProgressBa.setMinimum(0);
		luIndexProgressBa.setMaximum(100);
		luIndexProgressBa.setValue(0);
		luIndexProgressBa.setStringPainted(true);
		luIndexProgressBa.addChangeListener(this);
		luIndexProgressBa.setBorderPainted(true);
		luIndexProgressBa.setBackground(Color.GREEN);
		getContentPane().add(luIndexProgressBa);
		extKWJlabel = new JLabel("关键字提取：");
		extKWJlabel.setBounds(32, 70, 80, 15);
		getContentPane().add(extKWJlabel);
		extKWProgressBar = new JProgressBar();
		extKWProgressBar.setBounds(137, 70, 311, 22);
		extKWProgressBar.setMinimum(0);
		extKWProgressBar.setMaximum(100);
		extKWProgressBar.setValue(0);
		extKWProgressBar.setStringPainted(true);
		extKWProgressBar.addChangeListener(this);
		extKWProgressBar.setBorderPainted(true);
		extKWProgressBar.setBackground(Color.GREEN);
		getContentPane().add(extKWProgressBar);
		indexJLabel = new JLabel("索引构建：");
		indexJLabel.setBounds(32, 111, 80, 15);
		getContentPane().add(indexJLabel);
		indexProgressBar = new JProgressBar();
		indexProgressBar.setBounds(137, 111, 311, 22);
		indexProgressBar.setMinimum(0);
		indexProgressBar.setMaximum(100);
		indexProgressBar.setValue(0);
		indexProgressBar.setStringPainted(true);
		indexProgressBar.addChangeListener(this);
		indexProgressBar.setBorderPainted(true);
		indexProgressBar.setBackground(Color.GREEN);
		getContentPane().add(indexProgressBar);
		luIndextimer=new Timer(10, this);
		indexTimer=new Timer(10,this);
		extTimer=new Timer(10, this);
		luIndextimer.start();
		indexTimer.start();
		extTimer.start();
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//fileUpDeaProgress frame = new fileUpDeaProgress();
					//frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==luIndextimer){
			 int value = luIndexProgressBa.getValue();
	           if (value < lOperation.getDocNum()){
	        	   double finish=lOperation.getDealedNum()*100.0/lOperation.getDocNum();
	        	   luIndexProgressBa.setValue((int)finish);
	           }
			
		}else if(e.getSource()==extTimer){
			 int value = extKWProgressBar.getValue();
	           if (value < selectWord.getDocNum()){
	        	   double finish=selectWord.getDealedNum()*100.0/selectWord.getDocNum();
	        	   extKWProgressBar.setValue((int)finish);
	           }
		}else if(e.getSource()==indexTimer){
			 int value = indexProgressBar.getValue();
	           if (value <=buIndex.getDocNum()){
	        	   double finish=buIndex.getDealedNum()*100.0/buIndex.getDocNum();
	        	   indexProgressBar.setValue((int)finish);
	           }  
		}
		
	}
	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==luIndexProgressBa){
			int value = luIndexProgressBa.getValue();
			if(value>=100){
				luFinish=true;
			}
		}else if(e.getSource()==extKWProgressBar){
			int value=extKWProgressBar.getValue();
			if(value>=100){
				extFinish=true;
			}
		}else if(e.getSource()==indexProgressBar){
			int value=indexProgressBar.getValue();
			if(value>=100){
				indexFinish=true;
			}
		}
		if(luFinish&&extFinish&&indexFinish){
			JOptionPane.showMessageDialog(this, "索引构建成功！",
					"提示",JOptionPane.INFORMATION_MESSAGE);
		}

		
		
	}
}
